#include "ATMOS_NetworkManagerClient.h"
#include "ATMOS_NetworkConnectionInfo.h"
#include "ATMOS_PacketID.h"
#include "ATMOS_Packet.h"

#include "RakPeerInterface.h"
#include "MessageIdentifiers.h"
#include "BitStream.h"

#include <sstream>
#include <iostream>

using namespace ATMOS;

NetworkManagerClient::NetworkManagerClient()
{
}

NetworkManagerClient::~NetworkManagerClient()
{
}

const std::string &NetworkManagerClient::getServerGUID() const
{
	return mServerGUID;
}

void NetworkManagerClient::initialize( const NetworkConnectionInfo &info )
{
	NetworkManager::initialize( info );

	ATMOS_LOG_FUNC();
	RakNet::SocketDescriptor socketDescriptor( info.clientPort, 0 );

	mPeerInterface = RakNet::RakPeerInterface::GetInstance();
	mPeerInterface->AllowConnectionResponseIPMigration( false );
	
	mPeerInterface->Startup( 1, &socketDescriptor, 1 );
	mPeerInterface->SetOccasionalPing( true );
	mPeerInterface->SetMaximumIncomingConnections( info.maxConnections );

	ATMOS_LOG("connecting...");
	if( mPeerInterface->Connect( 
		info.serverIP.c_str(),
		info.serverPort,
		info.connectionPassword.c_str(),
		strlen( info.connectionPassword.c_str() ) ) !=
		RakNet::CONNECTION_ATTEMPT_STARTED )
	{
		ATMOS_LOG("failed to connect");
		ATMOS_THROW_RT("unable to connect to server");
	}
}